package C014;

import java.util.Stack;

/**
 * ClassName: StackImpleQueue
 * Package: C014
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/3 15:47
 * @Version 1.0
 * @Since 1.0
 */
public class StackImpleQueue {
    /*

    void push(int x) 将元素 x 推到队列的末尾
    int pop() 从队列的开头移除并返回元素
    int peek() 返回队列开头的元素
    boolean empty() 如果队列为空，返回 true ；否则，返回 false

     */
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.push(1); // queue is: [1]
        myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
        System.out.println(myQueue.peek()); // return 1
        System.out.println(myQueue.pop()); // return 1, queue is [2]
        System.out.println(myQueue.empty()); // return false
    }


    static class MyQueue {
        Stack<Integer> in = new Stack<>();
        Stack<Integer> out = new Stack<>();

        public MyQueue() {

        }

        public void inToOut() {
            if (!out.isEmpty()) {
                return;
            }
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }

        public void push(int x) {
            inToOut();
            in.push(x);
        }

        public int pop() {
            inToOut();
            return out.pop();
        }

        public int peek() {
            inToOut();
            return out.peek();
        }

        public boolean empty() {
            inToOut();
            return out.isEmpty();
        }
    }
}
